﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DoAnOOP.DataAccessLayer;
using DoAnOOP.BussinessLayer;
using DevComponents.DotNetBar;

namespace DoAnOOP.NQL
{
    public partial class frmQLGV : DevComponents.DotNetBar.Office2007Form
    {

        #region Hiển thị
        private void frmGV_Load(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM GIAOVIEN";
            clsConnect gv = new clsConnect();
            dataGridViewGV.DataSource = gv.GetDataTable(sql);
            KiemTraChinhSua(false);
            dataGridViewGV.Columns[0].HeaderText = "MGV";
            dataGridViewGV.Columns[1].HeaderText = "Họ";
            dataGridViewGV.Columns[2].HeaderText = "Tên";
            dataGridViewGV.Columns[3].HeaderText = "Học vị";
            dataGridViewGV.Columns[4].HeaderText = "Học hàm";
            dataGridViewGV.Columns[5].HeaderText = "Giới tính";
            dataGridViewGV.Columns[6].HeaderText = "Ngày sinh";
            dataGridViewGV.Columns[7].HeaderText = "Nơi sinh";
            dataGridViewGV.Columns[8].HeaderText = "Ngày vào làm";
            dataGridViewGV.Columns[9].HeaderText = "Khoa";

        }

        private void dataGridViewGV_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int numRecord = e.RowIndex;
                txtMaGV.Text = dataGridViewGV.Rows[numRecord].Cells[0].Value.ToString();
                txtHo.Text = dataGridViewGV.Rows[numRecord].Cells[1].Value.ToString();
                txtTen.Text = dataGridViewGV.Rows[numRecord].Cells[2].Value.ToString();
                txtHocvi.Text = dataGridViewGV.Rows[numRecord].Cells[3].Value.ToString();
                txtHocham.Text = dataGridViewGV.Rows[numRecord].Cells[4].Value.ToString();
                if (dataGridViewGV.Rows[numRecord].Cells[5].Value.ToString() == "Nam")
                    rdiNam.Checked = true;
                else
                    rdiNu.Checked = true;
                pickNgaySinh.Text = dataGridViewGV.Rows[numRecord].Cells[6].Value.ToString();
                txtNoiSinh.Text = dataGridViewGV.Rows[numRecord].Cells[7].Value.ToString();
                pickNgayvaolam.Text = dataGridViewGV.Rows[numRecord].Cells[8].Value.ToString();
                cboKhoa.Text = dataGridViewGV.Rows[numRecord].Cells[9].Value.ToString();
            }
            catch (Exception)
            {

            }
            
        }
        #endregion

        #region Hàm trong form
        private void KiemTraChinhSua(bool isEditting)
        {
            bntThem.Enabled = !isEditting;
            btnSua.Enabled = !isEditting;
            btnXoa.Enabled = !isEditting;
            btnXuat.Enabled = !isEditting;
            btnLuu.Enabled = isEditting;
            btnKhongluu.Enabled = isEditting;
            
            txtMaGV.Enabled = isEditting;
            txtHo.Enabled = isEditting;
            txtTen.Enabled = isEditting;
            txtNoiSinh.Enabled = isEditting;
            txtHocvi.Enabled = isEditting;
            txtHocham.Enabled = isEditting;
            pickNgaySinh.Enabled = isEditting;
            pickNgayvaolam.Enabled = isEditting;
            cboKhoa.Enabled = isEditting;
            rdiNam.Enabled = isEditting;
            rdiNu.Enabled = isEditting;
        }

        private void Reset()
        {
            txtMaGV.Text = "";
            txtHo.Text = "";
            txtTen.Text = "";
            txtHocvi.Text = "";
            txtHocham.Text = "";
            txtNoiSinh.Text = "";
            pickNgaySinh.Text = "";
            pickNgayvaolam.Text = "";
        }
        private string KiemTraLuu()
        {
            string kiemTra = "";
            if (txtMaGV.Text.Equals(""))
                kiemTra += "Chưa nhập Mã GV.\n";
            if (txtHo.Text.Equals(""))
                kiemTra += "Chưa nhập Họ sinh viên.\n";
            if (txtTen.Text.Equals(""))
                kiemTra += "Chưa nhập Tên sinh viên.\n";
            if (txtNoiSinh.Text.Equals(""))
                kiemTra += "Chưa nhập Nơi sinh.";
           
            return kiemTra;
        }
        #endregion

        #region Hàm của các button
        int flags;

        public frmQLGV()
        {
            InitializeComponent();
        }

        private void bntThem_Click(object sender, EventArgs e)
        {
            KiemTraChinhSua(true);
            Reset();
            flags = 1;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flags = 2;
            string temp = txtMaGV.Text;
            KiemTraChinhSua(true);
            txtMaGV.Enabled = false;
            txtMaGV.Text = temp; 
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            clsQLGV gvBUS = new clsQLGV();

            gvBUS.MaGV = txtMaGV.Text;  
            gvBUS.Ho = txtHo.Text;
            gvBUS.Ten = txtTen.Text;
            gvBUS.HocVi = txtHocvi.Text;
            gvBUS.HocHam = txtHocham.Text;
            gvBUS.GioiTinh = (rdiNam.Checked == true ? "Nam" : "Nữ");
            gvBUS.MaKhoa = cboKhoa.Text;
            gvBUS.NgaySinh = pickNgaySinh.Text;
                ;
            gvBUS.Ngayvaolam = pickNgaySinh.Text;
            gvBUS.NoiSinh = txtNoiSinh.Text;

            if (KiemTraLuu().Equals(""))
            {
                switch (flags)
                {
                    case 1: //Insert
                        gvBUS.ThemThongTinGiaoVien();
                        break;
                    case 2: //Update
                        gvBUS.UpdateThongTinGiaoVien();
                        break;
                    case 3: //Delete
                        gvBUS.DeleteThongTinGiaoVien();
                        break;
                }
                frmGV_Load(sender, e);
                KiemTraChinhSua(false);
            }
            else
                MessageBox.Show(KiemTraLuu().ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void btnKhongluu_Click(object sender, EventArgs e)
        {
            KiemTraChinhSua(false);
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("Bạn có chắc rằng mình muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
            {
                flags = 3;
                btnLuu_Click(sender, e);
            }
        }
        #endregion

    }
}